﻿namespace ApplicationFrame.EntityFrameworkCore.Core
{
    /// <summary>
    /// Hash分表数量（必须是2的幂次方，便于均匀分布）
    /// </summary>
    [AttributeUsage(AttributeTargets.Class)]
    public class HashShardingAttribute : Attribute
    {
        /// <summary>
        /// 分表数量（必须是2的幂次方，便于均匀分布）
        /// </summary>
        public int TableCount { get; }

        /// <summary>
        /// 分表实体属性名称
        /// </summary>
        public string ShardingProperty { get; }

        public HashShardingAttribute(string shardingProperty, int tableCount = 16)
        {
            if (string.IsNullOrWhiteSpace(shardingProperty))
                throw new ArgumentNullException(nameof(shardingProperty));
            ShardingProperty = shardingProperty;
            if (tableCount <= 0 || (tableCount & (tableCount - 1)) != 0)
                throw new ArgumentException("分表数量必须是正整数且为2的幂次方（如2、4、8、16）", nameof(tableCount));
            TableCount = tableCount;
        }
    }
}
